/******************************************************************************
 * Copyright (c) 2014 - 2016. Danlu.com Co.Ltd. All rights reserved.          *
 ******************************************************************************/

package cn.com.dhc.danlu.activity.coupon;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import java.math.BigDecimal;
import java.util.ArrayList;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.coupon.callback.OnCheckChangeCallback;
import cn.com.dhc.danlu.activity.delivery.DeliveryDetailActivity;
import cn.com.dhc.danlu.activity.goods.GoodInfoActivity;
import cn.com.dhc.danlu.util.CalculatePriceUtil;
import cn.com.dhc.danlu.util.CommonConst;
import cn.com.dhc.danlu.util.CommonConst.CouponConst;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.dhc.danlu.widget.couponview.CouponUseState;
import cn.com.dhc.danlu.widget.couponview.OnRightItemClick;
import cn.com.dhc.danlu.widget.couponview.filterview.FilterType;
import cn.com.hd.mall.web.webservices.entity.freedom.entity.coupon.CouponEntity;

import static cn.com.dhc.danlu.util.CommonConst.CouponConst.DEALERS_NOT_USE;
import static cn.com.dhc.danlu.widget.couponview.filterview.FilterType.dealers_coupon;

/**
 * @author: 李一方
 * @version: 1.0.0
 * @since: 2016-09-22 14:09
 * @mail: liyifang@danlu.com
 * @desc: 使用经销商红包 & 我的优惠券 & 经销商红包
 */
class DealersCouponUsedAdapter extends BaseDealersCouponAdapter<DealersCouponUsedAdapter.ViewHolder> {
    private OnCheckChangeCallback mCheckChangeCallback;
    private boolean isUsable;

    DealersCouponUsedAdapter (Context context, FilterType type, boolean isUsable) {
        super (context, type);
        this.isUsable = isUsable;
    }

    void setCheckChangeCallback (OnCheckChangeCallback mCheckChangeCallback) {
        this.mCheckChangeCallback = mCheckChangeCallback;
    }

    @Override
    protected ViewHolder createHolder (View view) {
        return new ViewHolder (view);
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ViewHolder extends BaseDealersCouponAdapter.BaseDealersCouponHolder {
        public ViewHolder (View view) {
            super (view);
        }

        @Override
        public void refreshItemView (int pos) {
            final CouponEntity entity = (CouponEntity) getItemByPos (pos);
            ArrayList<String> limits = new ArrayList<> ();
            String dealerLimit = entity.getDealerLimit ();
            if (!TextUtils.isEmpty (dealerLimit)) {
                limits.add ("限购" + entity.getDealerLimit ());
            }
            String channelLimit = entity.getChannelLimit ();
            if (!TextUtils.isEmpty (channelLimit)) {
                limits.add ("限购" + entity.getChannelLimit ());
            }
            if (!TextUtils.isEmpty (entity.getMerchLimit ())) {
                limits.add ("限购" + entity.getMerchLimit ());
            }
            String platformLimit = entity.getPlatformLimit ();
            if (!TextUtils.isEmpty (platformLimit)) {
                limits.add (entity.getPlatformLimit ());
            }
            String amtUseLimit = entity.getAmtUseLimit ();
            if (!TextUtils.isEmpty (amtUseLimit)) {
                if ("0".equals (amtUseLimit)) {
                    limits.add ("无使用金额限制");
                } else {
                    limits.add (String.format ("满%s元可用", CalculatePriceUtil.converseAmount (CommonUtils.fenToYuan (new BigDecimal (amtUseLimit)).toString ())));
                }
            }
            String price = CalculatePriceUtil.converseAmount (CommonUtils.fenToYuan (new BigDecimal (entity.getCouponAmt ())).toPlainString ());

            switch (mFilterType) {
                //我的优惠券
                case dealers_coupon:
                    setDealerCoupon (pos, entity, limits, price);
                    break;

                // 经销商红包 & 使用经销商红包
                case dealers_envelop:
                    dlEnvelop.setMarkState (false);
                    setDealerEnvelope (pos, entity, limits, price);
                    break;

                case dealers_envelop_use:
                    dlEnvelop.setMarkState (isUsable);
                    setDealerEnvelope (pos, entity, limits, price);
                    break;
            }

            if (mFilterType == dealers_coupon) {
                //我的优惠券
                setDealerCoupon (pos, entity, limits, price);
            } else {
                setDealerEnvelope (pos, entity, limits, price);
            }
        }

        /**
         * 设置经销商优惠券相关界面显示
         */
        private void setDealerCoupon (int pos, final CouponEntity entity, ArrayList<String> limits, String price) {
            dlYhq.setLimitDetail (limits);
            dlYhq.setPrice (price);
            dlYhq.setQuota (entity.getCouponName ());
            String couponStatus = entity.getCouponStatus ();
            String numOrderNo = null;
            dlYhq.setActiveTime (getFixedDiffTime (entity.getUseDate (), entity.getExpireDate (), entity.getTimeLeft (), couponStatus));

            switch (couponStatus) {
                case DEALERS_NOT_USE:
                    dlYhq.setUseState (CouponUseState.usedNot);
                    numOrderNo = "优惠券编号: " + entity.getCouponCode ();
                    dlYhq.setOnRightItemClick (new OnRightItemClick () {
                        @Override
                        public void onRightItemClick (View view, int resId) {
                            if (resId == R.id.sv_right) {
                                // 进入到商品详情页
                                Intent intent = new Intent (context, GoodInfoActivity.class);
                                intent.addFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                                intent.putExtra ("merchandiseId", entity.getMerchId ());
                                context.startActivity (intent);
                            }
                        }
                    });
                    break;

                case CouponConst.DEALERS_HAS_USED:
                    dlYhq.setUseState (CouponUseState.usedOver);
                    numOrderNo = "优惠券编号: " + entity.getCouponCode () + "\n使用订单: " + entity.getOrderNo ();
                    dlYhq.setOnRightItemClick (new OnRightItemClick () {
                        @Override
                        public void onRightItemClick (View view, int resId) {
                            if (resId == R.id.fl_order) {
                                // 跳转 -> 订单详情
                                toDeliveryDetail (entity.getOrderNo ());
                            }
                        }
                    });
                    break;

                case CouponConst.DEALERS_OUT_DATE:
                    numOrderNo = "优惠券编号: " + entity.getCouponCode ();
                    dlYhq.setUseState (CouponUseState.dateOver);
                    break;
            }

            dlYhq.setRestrict (numOrderNo);
            dlYhq.setOnExpandListener (new CouponExpandListener (pos));
        }

        /**
         * 设置经销商红包相关的界面显示
         */
        private void setDealerEnvelope (int pos, final CouponEntity entity, ArrayList<String> limits, String price) {
            dlEnvelop.setOnExpandListener (new CouponExpandListener (pos));
            // 经销商红包  已经使用、未使用、已经过期
            String couponStatus = entity.getCouponStatus ();
            if (mFilterType == FilterType.dealers_envelop_use) {
                dlEnvelop.setRestrict ("红包编号: " + entity.getCouponCode ());
                if (isUsable) {
                    dlEnvelop.setIsChecked (entity.isChecked ());
                    dlEnvelop.setOnClickListener (new View.OnClickListener () {
                        @Override
                        public void onClick (View v) {
                            if (null != mCheckChangeCallback) {
                                mCheckChangeCallback.onSelected (entity, (ImageView) v.findViewById (R.id.iv_markup));
                            }
                        }
                    });
                }
                // 不可用红包置灰 这里通过变更红包状态 将其复用到"过期"的券标识 从而达到置灰的目的，并将右上角的 logo 隐藏掉 2016-10-21 17:43:51 lyf
                if (!isUsable) {
                    couponStatus = CouponConst.DEALERS_OUT_DATE;
                }
            }
            String numOrderNo = null;
            dlEnvelop.setActiveTime (getFixedDiffTime (entity.getUseDate (), entity.getExpireDate (), entity.getTimeLeft (), couponStatus));

            switch (couponStatus) {
                case DEALERS_NOT_USE:// 未使用
                    numOrderNo = "红包编号: " + entity.getCouponCode ();
                    dlEnvelop.setUseState (CouponUseState.usedNot);
                    break;

                case CouponConst.DEALERS_HAS_USED://已经使用
                    dlEnvelop.setUseState (CouponUseState.usedOver);
                    numOrderNo = "红包编号: " + entity.getCouponCode () + "\n使用订单: " + entity.getOrderNo ();
                    dlEnvelop.setOnRightItemClick (new OnRightItemClick () {
                        @Override
                        public void onRightItemClick (View view, int resId) {
                            if (resId == R.id.fl_order) {
                                toDeliveryDetail (entity.getOrderNo ());
                            }
                        }
                    });
                    break;

                case CouponConst.DEALERS_OUT_DATE://已过期
                    dlEnvelop.setUseState (CouponUseState.dateOver);
                    numOrderNo = "红包编号: " + entity.getCouponCode ();
                    if (!isUsable) {
                        dlEnvelop.setIvUseStateVisible (View.GONE);
                    }
                    break;
            }
            dlEnvelop.setRestrict (numOrderNo);
            dlEnvelop.setPrice (price);
            dlEnvelop.setLimitDetail (limits);
            dlEnvelop.setQuota (entity.getCouponName ()); //红包名称
        }

        /**
         * 获取修正后的日期显示
         *
         * @param useDate
         *         使用时间
         * @param expireDate
         *         过期时间
         * @param timeLeft
         *         剩余时间
         * @param type
         *         控制是否显示"(剩余 xx 天)"
         *
         * @return 修正后的日期字符串
         */
        @NonNull
        private String getFixedDiffTime (String useDate, String expireDate, String timeLeft, String type) {
            String diffTime;
            switch (type) {
                case DEALERS_NOT_USE:
                    if (Integer.parseInt (timeLeft) <= 3) {
                        diffTime = expireDate + "到期(仅剩" + timeLeft + "天)";
                    } else {
                        diffTime = useDate + " 至 " + expireDate;
                    }
                    break;
                default:
                    diffTime = useDate + " 至 " + expireDate;
                    break;
            }
            return diffTime;
        }

        /**
         * 跳转订单详情页面
         *
         * @param orderNo
         *         订单号
         */
        private void toDeliveryDetail (String orderNo) {
            Intent intent = new Intent (context, DeliveryDetailActivity.class);
            intent.putExtra (CommonConst.MyOrderConst.SMALL_ORDER, orderNo);
            context.startActivity (intent);
        }
    }
}